package ar.com.jsejtman.cajugastos2.core.service;

import ar.com.jsejtman.cajugastos2.core.dao.EmpresaDao;
import ar.com.jsejtman.cajugastos2.core.entity.Empresa;
import ar.com.jsejtman.common.persistence.exception.DaoException;
import ar.com.jsejtman.common.service.data.DataPage;
import ar.com.jsejtman.common.service.data.SortCriterion;
import ar.com.jsejtman.common.service.definition.BaseService;
import ar.com.jsejtman.common.service.exception.EntitySearchException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Permite manipular empresas.
 * 
 * @author jsejtman
 */
@Service
@Transactional
public class EmpresaService extends BaseService implements IEmpresaService {

	@Override
	public final DataPage<Empresa> searchEmpresa(final String cuit, final String razonSocial,
	        final Integer pageNumber, final Integer pageSize, final SortCriterion sortCriterion)
	        throws EntitySearchException {

		try {
			if (StringUtils.isBlank(cuit) && StringUtils.isBlank(razonSocial)) {
				throw new EntitySearchException("Se requiere al menos un criterio de búsqueda");
			}

			final EmpresaDao dao = this.getDaoFactory().newDao(EmpresaDao.class);

			return (dao.searchEmpresa(cuit, razonSocial, pageNumber, pageSize, sortCriterion));

		} catch (final DaoException e) {
			throw new EntitySearchException(e.getMessage(), e);
		}
	}

}
